package com.ruoyi.leaveManage.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.UserSchoolInformation;
import com.ruoyi.leaveManage.bo.LeaveManageAddBo;
import com.ruoyi.leaveManage.bo.LeaveManageEditBo;
import com.ruoyi.leaveManage.bo.LeaveManageQueryBo;
import com.ruoyi.leaveManage.domain.LeaveManage;
import com.ruoyi.leaveManage.domain.LeaveUser;
import com.ruoyi.leaveManage.mapper.LeaveManageMapper;
import com.ruoyi.leaveManage.service.ILeaveManageService;
import com.ruoyi.leaveManage.service.ILeaveUserService;
import com.ruoyi.leaveManage.vo.LeaveUserVo;
import com.ruoyi.leaveManage.vo.LeaveVo;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 请假信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-04-26
 */
@Service
public class LeaveManageServiceImpl extends ServiceImpl<LeaveManageMapper, LeaveManage> implements ILeaveManageService {

    @Autowired
    private  ISysUserService iSysUserService;
    @Autowired
    private ILeaveUserService iLeaveUserService;
    @Override
    public LeaveVo queryById(Long leaveId){
        LeaveManage leaveManage = this.baseMapper.selectById(leaveId);
        LeaveUser leaveUser = iLeaveUserService.getById(leaveManage.getLeaveId());
        LeaveVo leaveVo=BeanUtil.toBean(leaveManage, LeaveVo.class);
        leaveVo.setStartTime(leaveUser.getStartTime());
        leaveVo.setEndTime(leaveUser.getEndTime());
        leaveVo.setRemark(leaveUser.getRemark());
        leaveVo.setCancellationStatus(leaveUser.getCancellationStatus());
        return leaveVo;
    }



    @Override
    public List<LeaveVo> queryList(LeaveManageQueryBo bo) {
        LambdaQueryWrapper<LeaveManage> lqw = Wrappers.lambdaQuery();

        lqw.orderByAsc(LeaveManage::getCreateTime);
        lqw.eq(bo.getUserId() != null,LeaveManage::getUserId,bo.getUserId());
        lqw.eq(bo.getType() != null, LeaveManage::getType, bo.getType());
        lqw.eq(StrUtil.isNotBlank(bo.getHealthCondition()), LeaveManage::getHealthCondition, bo.getHealthCondition());
        lqw.eq(StrUtil.isNotBlank(bo.getDestination()), LeaveManage::getDestination, bo.getDestination());
        lqw.eq(StrUtil.isNotBlank(bo.getImage()), LeaveManage::getImage, bo.getImage());
        lqw.eq(StrUtil.isNotBlank(bo.getSelfPhone()), LeaveManage::getSelfPhone, bo.getSelfPhone());
        lqw.eq(bo.getHour() != null, LeaveManage::getHour, bo.getHour());
        lqw.eq(StrUtil.isNotBlank(bo.getBackupPhone()), LeaveManage::getBackupPhone, bo.getBackupPhone());
        lqw.eq(bo.getApproveStatusGuide() != null, LeaveManage::getApproveStatusGuide, bo.getApproveStatusGuide());
        lqw.eq(StrUtil.isNotBlank(bo.getApproveByGuide()), LeaveManage::getApproveByGuide, bo.getApproveByGuide());
        lqw.eq(bo.getApproveStatusLeading() != null, LeaveManage::getApproveStatusLeading, bo.getApproveStatusLeading());
        lqw.eq(StrUtil.isNotBlank(bo.getApproveByLeading()), LeaveManage::getApproveByLeading, bo.getApproveByLeading());
        List<LeaveVo> leaveVos =entity2Vo(this.list(lqw));

        LambdaQueryWrapper<SysUser> ulpw = Wrappers.lambdaQuery();
        String studentId="_";
        if (bo.getGrade()!=null||bo.getCollege()!=null||bo.getMajor()!=null){
            if (bo.getGrade()!=null) studentId+=bo.getGrade();
            else studentId+="__";
            if (bo.getCollege()!=null) studentId+=bo.getCollege();
            else studentId+="__";
            if (bo.getMajor()!=null) studentId+=bo.getMajor()+"____";
            else studentId+="______";
            ulpw.like(true,SysUser::getUserName,studentId);
        }

        List<LeaveVo> lv=new ArrayList<>();
        for (LeaveVo manageVo: leaveVos) {
            ulpw.like(true,SysUser::getUserId,manageVo.getUserId());
            LambdaQueryWrapper<LeaveUser> lulpw = Wrappers.lambdaQuery();
            lulpw.eq(bo.getCancellationStatus()==0,LeaveUser::getCancellationStatus,bo.getCancellationStatus());
            lulpw.eq(LeaveUser::getLeaveId,manageVo.getLeaveId());
            LeaveUser leaveUser = iLeaveUserService.getOne(lulpw);
            if (leaveUser!=null){
                manageVo.setLeaveUserId(leaveUser.getLeaveUserId());
                manageVo.setStartTime(leaveUser.getStartTime());
                manageVo.setEndTime(leaveUser.getEndTime());
                manageVo.setRemark(leaveUser.getRemark());
                manageVo.setCancellationStatus(leaveUser.getCancellationStatus());
            }
            SysUser user=iSysUserService.getOne(ulpw);
            if (user!=null){
                manageVo.setStudentId(user.getUserName());
                manageVo.setUserName(user.getNickName());
                UserSchoolInformation usi=iSysUserService.selectUserSchoolInformation(user.getUserName());
                manageVo.setGrade(usi.getGrade());
                manageVo.setCollege(usi.getCollege());
                manageVo.setMajor(usi.getMajor());
                manageVo.longString();
                lv.add(manageVo);
            }

        }
        return lv;
    }

    @Override
    public List<LeaveUserVo> collegeOrteacherQueryList(LeaveManageQueryBo bo) {

        return  baseMapper.collegeOrteacherQueryList(bo);
    }

    @Override
    public List<LeaveUserVo> getReportFromList(LeaveManageQueryBo bo) {
        return baseMapper.getReportFromList(bo);
    }

    @Override
    public Boolean updateApprove(LeaveManageEditBo bo) {
        LeaveManage leaveManage=this.getById(bo.getLeaveId());
        if (bo.getApproveStatusGuide()!=null)leaveManage.setApproveStatusGuide(bo.getApproveStatusGuide());
        if (bo.getApproveStatusLeading()!=null)leaveManage.setApproveStatusLeading(bo.getApproveStatusLeading());
        leaveManage.setRejectReason(bo.getRejectReason());
        return this.updateById(leaveManage);
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<LeaveVo> entity2Vo(Collection<LeaveManage> collection) {
        List<LeaveVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, LeaveVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<LeaveManage> page = (Page<LeaveManage>)collection;
            Page<LeaveVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(LeaveManageAddBo bo) {
        LeaveManage add = BeanUtil.toBean(bo, LeaveManage.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(LeaveManageEditBo bo) {
        LeaveManage update = BeanUtil.toBean(bo, LeaveManage.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(LeaveManage entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }
}
